package org.zfes.snowier.zbss.xzy.biz.message.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.data.SelectVo;

import org.zfes.snowier.zbss.xzy.biz.message.service.IMessageService;
import org.zfes.snowier.zbss.xzy.biz.vedio.model.CloudImage;
import org.zfes.snowier.zbss.xzy.biz.vedio.model.CloudVedio;
import org.zfes.snowier.zbss.xzy.biz.vedio.service.ICloudImageService;
import org.zfes.snowier.zbss.xzy.biz.vedio.service.ICloudVedioService;
import org.zfes.snowier.zbss.xzy.message.CloudImageDto;
import org.zfes.snowier.zbss.xzy.message.CloudVedioDto;
import org.zfes.snowier.zbss.xzy.message.MessageDto;
import org.zfes.snowier.zbss.xzy.message.MessageInfoDto;
import org.zfes.snowier.zbss.xzy.message.MessagePusDto;
import org.zfes.snowier.zbss.xzy.biz.message.dao.MessageMapper;
import org.zfes.snowier.zbss.xzy.biz.message.model.Message;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class MessageServiceImpl implements IMessageService{
 	@Autowired
	private MessageMapper messageMapper;
 	
 	@Autowired ICloudVedioService cloudVedioService;
 	@Autowired ICloudImageService cloudImageService;
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public Long alertMsg(MessagePusDto message) {
 		ZBeanUtil.validateBean(message);
 		Message msg=new Message();
 		ZBeanUtil.copy(message, msg);
 		Long id = createMessage(msg);
 		return id;
 		
 	}
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public Long createMessage(Message message) {
		ZBeanUtil.setCreateInfo(message);
		ZBeanUtil.validateBean(message);
		messageMapper.insertSelective(message);
		return message.getId();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Message> loadMessageById(Long id) {
		if(id!=null){
			return Optional.ofNullable(messageMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateMessage(Message message) {
		ZBeanUtil.setUpdatedInfo(message);
		ZBeanUtil.validateBean(message);
		messageMapper.updateByPrimaryKeySelective(message);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteMessage(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		messageMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMessageStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		messageMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadMessageDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("title").ifPresent(v->pm.updateParam("title", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(messageMapper.selectListCount(pm), messageMapper.selectMapListPage(pm));
		return ds;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SelectVo> loadMessageSelectVo(String keywords) {
		if(ZStrUtil.hasText(keywords)) {
			
		}
		List<SelectVo> voList=messageMapper.selectDropList(Byte.valueOf("1"));
		
		return voList==null?Lists.newArrayList():voList;
	}

	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<MessageInfoDto> loadMessageInfoById(Long id){
		if(id==null) {
			return Optional.empty();
		}
		MessageInfoDto res=new MessageInfoDto();
		loadMessageById( id).ifPresent(msg->{
			MessageDto message=new MessageDto();
			ZBeanUtil.copy(msg, message);
			res.setMessage(message);
		});
		List<CloudVedio>  vedioList=cloudVedioService.loaByMessageId(id, Byte.valueOf("1"));
		List<CloudImage>  imageList=cloudImageService.loaByMessageId(id, Byte.valueOf("1"));
		res.setVedios(vedioList.stream().map(v->{
							CloudVedioDto vdt=new CloudVedioDto();
							ZBeanUtil.copy(v, vdt);
							return vdt;
						}).collect(Collectors.toList()));
		
		res.setImages(imageList.stream().map(v->{
			CloudImageDto vdt=new CloudImageDto();
			ZBeanUtil.copy(v, vdt);
			return vdt;
		}).collect(Collectors.toList()));
		
		return Optional.ofNullable(res);
		
	}
    
}